/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;
using MonoTouch.CoreAnimation;

namespace ArtNotes
{	
	public class ANImageInfoController : UIViewController
	{
		public ANImageInfoController (IntPtr handle) : base(handle)
		{
		}
		
		private UIAlertView confirmDeleteAlert;
		
		private int imageId = 0;
		private Image image;
		private FlickrImage flickrImage;
		public ANImageInfoController (int ImageId)
		{
			this.imageId = ImageId;
			anDB = AN_APP_OPTIONS.AN_DATABASE;
			image = anDB.GetImageByID(imageId);
			
			//flickrImage = anDB.GetFlickrImageForImage(image);
			flickrImage = new FlickrImage();
		}
		
		private ANDatabase anDB;
		
		// From the monotouch samples
		private UIButton RoundedButtonType (float xpos, float ypos, string btitle)
		{
			var button = UIButton.FromType (UIButtonType.RoundedRect);
			button.Frame = new RectangleF (xpos, ypos, 106f, 40f);
			button.BackgroundColor = UIColor.Clear;
			button.SetTitle (btitle, UIControlState.Normal);
			button.SetTitleColor (UIColor.Black, UIControlState.Normal);

			button.Tag = 1;
			// To support reusable cells
			return button;
		}
		
		private UIButton ImageButtonType (float xpos, float ypos, UIImage defaultImage, UIImage pressedImage)
		{
			var background = defaultImage;//UIImage.FromFile("Images/btnSearch.png");
			var backgroundPressed = pressedImage;//UIImage.FromFile("Images/btnSearchPressed.png");
			var frame = new RectangleF(xpos,ypos,50f,50f);
			
			var button = new UIButton(frame);
			var nbackground = background.StretchableImage(12,0);
			var nbackgroundPressed = backgroundPressed.StretchableImage(12,0);
			button.SetBackgroundImage(nbackground,UIControlState.Normal);
			button.SetBackgroundImage(nbackgroundPressed,UIControlState.Highlighted);

			button.Tag = 1;
			return button;
		}
		
		private ANImageView anImageView;
		private ANImageMetaData anImageMetaData;
		private ANImageTag anImageTag;
		private ANFlickrInfo anFlickrInfo;
		
		private int anImageViewPos = 0;
		private int anImageMetaDataPos = 0;
		private int anImageTagsPos = 0;
		private int anFlickrInfoPos = 0;
		
		private int topSubViewPos = 0;
		
		private UIButton btnImage;
		private UIButton btnImageMeta;
		private UIButton btnImageTags;
		private UIButton btnFlickrInfo;
		
		private UIImageView tornPaperBG;
		
		public override void ViewDidLoad ()
		{	
			Console.WriteLine ("imageinfo view did load");

			anImageView = new ANImageView(image);
			anImageMetaData = new ANImageMetaData(image);
			anImageTag = new ANImageTag(image);
			anFlickrInfo = new ANFlickrInfo(flickrImage);
			
			tornPaperBG = new UIImageView();
			tornPaperBG.Image = UIImage.FromFile("Images/torn.png");
			tornPaperBG.Frame = new RectangleF(0,260,320,110);
			
			this.Title = image.Title;
			
			this.View.AddSubview(anFlickrInfo);
			this.anFlickrInfoPos = this.View.Subviews.Count() -1;
						
			this.View.AddSubview(anImageMetaData);
			this.anImageMetaDataPos = this.View.Subviews.Count() -1;
			this.View.AddSubview(anImageTag);
			this.anImageTagsPos = this.View.Subviews.Count() -1;
			this.View.AddSubview(anImageView);
			this.anImageViewPos = this.View.Subviews.Count() -1;
			
			this.topSubViewPos = this.View.Subviews.Count() -1;

			//btnImage = RoundedButtonType(0f,280f,"Image");
			//btnImageMeta = RoundedButtonType(107f,300f,"Meta");
			//btnImageTags = RoundedButtonType (214, 300,"Tags");
			ANImageUtils imageUtils = new ANImageUtils();
			
			this.View.AddSubview(tornPaperBG);
			
			UIImage frame = UIImage.FromFile("Images/polaroid.png");
			UIImage thumb = UIImage.FromFileUncached(AN_APP_OPTIONS.THUMB_DIR + image.ThumbFileName);
			UIImage composite = imageUtils.resizeImage( imageUtils.overlayImageWithPolaroid(
				thumb,
				frame,
				new RectangleF (0, 0, 75, 75),
				new RectangleF (0, 0, 75, 75),
				new SizeF (75, 75)
				), new SizeF (50, 50));
			
			btnImage = ImageButtonType(30f,300f, composite, composite);
			
			//UIImage metaButtonImg = UIImage.FromFile("Images/button_meta.png");
			UIImage metaButtonImg = imageUtils.resizeImage( imageUtils.overlayImageWithPolaroid(
				UIImage.FromFile("Images/metadata_generic.png"),
				frame,
				new RectangleF (0, 0, 75, 75),
				new RectangleF (0, 0, 75, 75),
				new SizeF (75, 75)
				), new SizeF (50, 50));
			//
			btnImageMeta = ImageButtonType(90,300, metaButtonImg, metaButtonImg);
			
			//UIImage tagButtonImg = UIImage.FromFile("Images/button_tag.png");
			UIImage tagButtonImg = imageUtils.resizeImage( imageUtils.overlayImageWithPolaroid(
				UIImage.FromFile("Images/tags.png"),
				frame,
				new RectangleF (0, 0, 75, 75),
				new RectangleF (0, 0, 75, 75),
				new SizeF (75, 75)
				), new SizeF (50, 50));
			//
			btnImageTags = ImageButtonType(150,300, tagButtonImg, tagButtonImg);

			UIImage flickrButtonImg = imageUtils.resizeImage( imageUtils.overlayImageWithPolaroid(
				UIImage.FromFile("Images/metadata_flickr.png"),
				frame,
				new RectangleF (0, 0, 75, 75),
				new RectangleF (0, 0, 75, 75),
				new SizeF (75, 75)
				), new SizeF (50, 50));
			//
			btnFlickrInfo = ImageButtonType(210,300, flickrButtonImg, flickrButtonImg);
			
			btnImage.TouchDown += switchViewFromButtonPress;
			btnImageMeta.TouchDown += switchViewFromButtonPress;
			btnImageTags.TouchDown += switchViewFromButtonPress;
			btnFlickrInfo.TouchDown += switchViewFromButtonPress;
			
			this.View.AddSubview(btnFlickrInfo);
			this.View.AddSubview(btnImageTags);
			this.View.AddSubview(btnImageMeta);
			this.View.AddSubview(btnImage);
			
			Random random = new Random ();
			float rotation = (float)(Math.PI * random.Next (-5, 5) / 180.0) ;
			btnFlickrInfo.Transform = CGAffineTransform.MakeRotation (rotation);
			rotation = (float)(Math.PI * random.Next (-5, 5) / 180.0) ;
			btnImageTags.Transform = CGAffineTransform.MakeRotation (rotation);
			rotation = (float)(Math.PI * random.Next (-5, 5) / 180.0) ;
			btnImageMeta.Transform = CGAffineTransform.MakeRotation (rotation);
			rotation = (float)(Math.PI * random.Next (-5, 5) / 180.0) ;
			btnImage.Transform = CGAffineTransform.MakeRotation (rotation);
			
			confirmDeleteAlert = createConfirmDelete();
			UIBarButtonItem btnRemove = new UIBarButtonItem();
			btnRemove.Title = "Remove";
			btnRemove.Clicked += delegate {
				confirmDeleteAlert.Show();
			};
			
			this.NavigationItem.SetRightBarButtonItem(btnRemove,true);
		}
		
		private UIAlertView createConfirmDelete()
		{
			UIAlertView uia = new UIAlertView();
			uia.Title = "Remove Image";
			uia.Message = "Are you sure you want to continue?";
			uia.AddButton("Yes");
			uia.AddButton("No");
			uia.Clicked += delegate(object sender, UIButtonEventArgs e) {
				if(e.ButtonIndex.Equals(0))
				{
					Console.WriteLine("Pressed!");
					//need to also delete the physical files!
					anDB.RemoveImage(image);
					//...
					this.NavigationController.PopViewControllerAnimated(true);
				}
			};
			return uia;
		}
		
		void removeImage(object sender, EventArgs e)
		{
			
		}
		
		void switchViewFromButtonPress(object sender, EventArgs e)
		{
			if(sender.Equals(btnImage))
			{
				if(this.anImageViewPos != this.topSubViewPos)
				{
					// this will always animate down
					this.beginAnimation(false);
					this.View.ExchangeSubview(this.anImageViewPos,this.topSubViewPos);
					this.finishAnimation();
					
					if(this.anImageMetaDataPos == this.topSubViewPos)
						this.anImageMetaDataPos = this.anImageViewPos;
					else if(this.anImageTagsPos == this.topSubViewPos)
						this.anImageTagsPos = this.anImageViewPos;
					else
						this.anFlickrInfoPos = this.anImageViewPos;
				
					this.anImageViewPos = this.topSubViewPos;
				}
			}
			else if(sender.Equals(btnImageMeta))
			{
				if(this.anImageMetaDataPos != this.topSubViewPos)
				{
					if(anImageViewPos != this.topSubViewPos)
						this.beginAnimation(false);
					else
						this.beginAnimation(true);
					
					this.View.ExchangeSubview(this.anImageMetaDataPos,this.topSubViewPos);
					this.finishAnimation();
					
					if(this.anImageTagsPos == this.topSubViewPos)
						this.anImageTagsPos = this.anImageMetaDataPos;
					else if(this.anFlickrInfoPos == this.topSubViewPos)
						this.anFlickrInfoPos = this.anImageMetaDataPos;
					else
						this.anImageViewPos = this.anImageMetaDataPos;
					
					this.anImageMetaDataPos = this.topSubViewPos;
				}
			}
			else if(sender.Equals(btnImageTags))
			{	
				if(this.anImageTagsPos != this.topSubViewPos)
				{
					if((anImageMetaDataPos != this.topSubViewPos) && (anImageViewPos != this.topSubViewPos))
						this.beginAnimation(false);
					else
						this.beginAnimation(true);
					
					this.View.ExchangeSubview(this.anImageTagsPos,this.topSubViewPos);
					this.finishAnimation();
					
					if(this.anFlickrInfoPos == this.topSubViewPos)
						this.anFlickrInfoPos = this.anImageTagsPos;
					else if(this.anImageViewPos == this.topSubViewPos)
						this.anImageViewPos = this.anImageTagsPos;
					else
						this.anImageMetaDataPos = this.anImageTagsPos;
					
					this.anImageTagsPos = this.topSubViewPos;
				}
			}
			else if(sender.Equals(btnFlickrInfo))
			{
				if(this.anFlickrInfoPos != this.topSubViewPos)
				{
					//this will always animate up
					this.beginAnimation(true);
					this.View.ExchangeSubview(this.anFlickrInfoPos,this.topSubViewPos);
					this.finishAnimation();
					
					if(this.anImageViewPos == this.topSubViewPos)
						this.anImageViewPos = this.anFlickrInfoPos;
					else if(this.anImageMetaDataPos == this.topSubViewPos)
						this.anImageMetaDataPos = this.anFlickrInfoPos;
					else
						this.anImageTagsPos = this.anFlickrInfoPos;
					
					this.anFlickrInfoPos = this.topSubViewPos;
				}
			}
		}
		
		private void beginAnimation(bool up)
		{
			UIView.BeginAnimations("");
			if(up)
				UIView.SetAnimationTransition(UIViewAnimationTransition.CurlUp,this.View,false);
			else
				UIView.SetAnimationTransition(UIViewAnimationTransition.CurlDown,this.View,false);
			UIView.SetAnimationCurve(UIViewAnimationCurve.EaseInOut);
			UIView.SetAnimationDuration(0.75);
		}
		
		private void finishAnimation()
		{
			UIView.CommitAnimations();
		}
 
		public override void ViewWillDisappear (bool animated)
		{
			Console.WriteLine ("imageinfo Hidden");
			this.anImageTag.saveChanges();
			this.anImageMetaData.saveChanges();
		}
	
		public override void ViewWillAppear (bool animated)
		{
			Console.WriteLine ("imageinfo Visible");

		}

	}
}
